1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.collect;
16
17 import static com.google.common.base.Preconditions.checkArgument;
18 import static com.google.common.base.Preconditions.checkNotNull;
19
20 import com.google.common.annotations.Beta;
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23
24 import java.util.Collections;
25 import java.util.NoSuchElementException;
26 import java.util.Set;
27
28
29
30
31
32
33
34
35
36
37
38
39 @Beta
40 @GwtCompatible(emulated = true)
41 @SuppressWarnings("rawtypes")
42 public abstract class ContiguousSet<C extends Comparable> extends ImmutableSortedSet<C> {
43
44
45
46
47
48
49
50
51
52 public static <C extends Comparable> ContiguousSet<C> create(
53 Range<C> range, DiscreteDomain<C> domain) {
54 checkNotNull(range);
55 checkNotNull(domain);
56 Range<C> effectiveRange = range;
57 try {
58 if (!range.hasLowerBound()) {
59 effectiveRange = effectiveRange.intersection(Range.atLeast(domain.minValue()));
60 }
61 if (!range.hasUpperBound()) {
62 effectiveRange = effectiveRange.intersection(Range.atMost(domain.maxValue()));
63 }
64 } catch (NoSuchElementException e) {
65 throw new IllegalArgumentException(e);
66 }
67
68
69 boolean empty = effectiveRange.isEmpty()
70 || Range.compareOrThrow(
71 range.lowerBound.leastValueAbove(domain),
72 range.upperBound.greatestValueBelow(domain)) > 0;
73
74 return empty
75 ? new EmptyContiguousSet<C>(domain)
76 : new RegularContiguousSet<C>(effectiveRange, domain);
77 }
78
79 final DiscreteDomain<C> domain;
80
81 ContiguousSet(DiscreteDomain<C> domain) {
82 super(Ordering.natural());
83 this.domain = domain;
84 }
85
86 @Override public ContiguousSet<C> headSet(C toElement) {
87 return headSetImpl(checkNotNull(toElement), false);
88 }
89
90
91
92
93 @GwtIncompatible("NavigableSet")
94 @Override public ContiguousSet<C> headSet(C toElement, boolean inclusive) {
95 return headSetImpl(checkNotNull(toElement), inclusive);
96 }
97
98 @Override public ContiguousSet<C> subSet(C fromElement, C toElement) {
99 checkNotNull(fromElement);
100 checkNotNull(toElement);
101 checkArgument(comparator().compare(fromElement, toElement) <= 0);
102 return subSetImpl(fromElement, true, toElement, false);
103 }
104
105
106
107
108 @GwtIncompatible("NavigableSet")
109 @Override public ContiguousSet<C> subSet(C fromElement, boolean fromInclusive, C toElement,
110 boolean toInclusive) {
111 checkNotNull(fromElement);
112 checkNotNull(toElement);
113 checkArgument(comparator().compare(fromElement, toElement) <= 0);
114 return subSetImpl(fromElement, fromInclusive, toElement, toInclusive);
115 }
116
117 @Override public ContiguousSet<C> tailSet(C fromElement) {
118 return tailSetImpl(checkNotNull(fromElement), true);
119 }
120
121
122
123
124 @GwtIncompatible("NavigableSet")
125 @Override public ContiguousSet<C> tailSet(C fromElement, boolean inclusive) {
126 return tailSetImpl(checkNotNull(fromElement), inclusive);
127 }
128
129
130
131
132 abstract ContiguousSet<C> headSetImpl(C toElement, boolean inclusive);
133
134 abstract ContiguousSet<C> subSetImpl(C fromElement, boolean fromInclusive,
135 C toElement, boolean toInclusive);
136
137 abstract ContiguousSet<C> tailSetImpl(C fromElement, boolean inclusive);
138
139
140
141
142
143
144
145 public abstract ContiguousSet<C> intersection(ContiguousSet<C> other);
146
147
148
149
150
151
152
153 public abstract Range<C> range();
154
155
156
157
158
159
160
161
162
163
164
165
166 public abstract Range<C> range(BoundType lowerBoundType, BoundType upperBoundType);
167
168
169 @Override public String toString() {
170 return range().toString();
171 }
172
173
174
175
176
177
178
179
180
181 @Deprecated public static <E> ImmutableSortedSet.Builder<E> builder() {
182 throw new UnsupportedOperationException();
183 }
184 }